package com.rimi.util;

import com.alibaba.druid.pool.DruidDataSource;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 连接池工具类
 * 提供Connection
 * @author chengliang
 */
public class DruidUtil {

    /**
     * 连接池
     */
    private final static DruidDataSource druidDataSource = new DruidDataSource();

    static {
        //获取文件
        Properties properties = new Properties();
        //获取src目录下的文件
        InputStream resourceAsStream = DruidUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
        try {
            //加载配置文件
            properties.load(resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //设置连接池参数
        druidDataSource.setConnectProperties(properties);
    }

    public static Connection getConnection(){
        try {
            return druidDataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //出现异常返回NULL
        return null;
    }

}
